﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyForum.DataObjects;
using TinyForum.Domain.Model;

namespace TinyForum.Application
{
    public static class DataObjectMapper
    {
        public static AccountDataObject MapToDataObject(Account user)
        {
            return new AccountDataObject
            {
                Avatar = user.Avatar,
                DateJoined = user.DateJoined,
                DateLastLogon = user.DateLastLogon,
                DisplayName = user.DisplayName,
                Email = user.Email,
                IsAdmin = user.IsAdmin,
                Password = user.Password,
                UserName = user.UserName,
                DateLastLocked = user.DateLastLocked,
                DateLastPasswordChanged = user.DateLastPasswordChanged,
                ID = user.ID,
                IsApproved = user.IsApproved,
                IsLocked = user.IsLocked,
                IsOnline = user.IsOnline,
                PasswordAnswer = user.PasswordAnswer,
                PasswordQuestion = user.PasswordQuestion
            };
        }

        public static TopicDataObject MapToDataObject(Topic topic)
        {
            return new TopicDataObject
            {
                ID = topic.ID,
                Name = topic.Name,
                Description = topic.Description
            };
        }

        public static PostDataObject MapToDataObject(Post post, Account user)
        {
            return new PostDataObject
            {
                Content = post.Content,
                DateCreated = post.DateCreated,
                DateModified = post.DateModified,
                ID = post.ID,
                User = MapToDataObject(user),
                Title = post.Title,
                TopicID = post.TopicID
            };
        }

        public static PostDataObject MapToDataObject(Post post, Reply latestReply, Account postUser, Account latestReplyUser, int? totalReplies)
        {
            PostDataObject postDataObject = new PostDataObject
            {
                Content = post.Content,
                DateCreated = post.DateCreated,
                DateModified = post.DateModified,
                ID = post.ID,
                User = MapToDataObject(postUser),
                Title = post.Title,
                TopicID = post.TopicID
            };
            if (latestReply == null)
                postDataObject.LatestReply = null;
            else
                postDataObject.LatestReply = MapToDataObject(latestReply, latestReplyUser);
            postDataObject.TotalReplies = totalReplies;
            return postDataObject;
        }

        public static ReplyDataObject MapToDataObject(Reply reply, Account user)
        {
            return new ReplyDataObject
            {
                Content = reply.Content,
                DateCreated = reply.DateCreated,
                DateModified = reply.DateModified,
                ID = reply.ID,
                PostID = reply.PostID,
                User = MapToDataObject(user),
                Title = reply.Title
            };
        }

        public static TopicDetailDataObject MapToDataObject(Topic topic, Post latestPost, Reply latestReply, Account latestPostUser, Account latestReplyUser, int numOfPosts)
        {
            TopicDetailDataObject topicDetailDataObject = new TopicDetailDataObject();
            topicDetailDataObject.Topic = MapToDataObject(topic);
            topicDetailDataObject.NumOfPosts = numOfPosts;
            if (latestPost != null)
                topicDetailDataObject.LatestPost = MapToDataObject(latestPost, latestPostUser);
            if (latestReply != null)
                topicDetailDataObject.LatestReply = MapToDataObject(latestReply, latestReplyUser);
            return topicDetailDataObject;
        }

        public static SettingDataObject MapToDataObject(Setting setting)
        {
            return new SettingDataObject
            {
                ID = setting.ID,
                PostsPerPage = setting.PostsPerPage,
                RepliesPerPage = setting.RepliesPerPage,
                SiteCopyright = setting.SiteCopyright,
                SiteDescription = setting.SiteDescription,
                SiteTitle = setting.SiteTitle,
                SiteTheme = setting.SiteTheme
            };
        }

        public static PostDetailDataObject MapToDataObject(Post post, Account user, IEnumerable<Tuple<Reply, Account>> replies)
        {
            PostDetailDataObject result = new PostDetailDataObject();
            result.Post = MapToDataObject(post, user);
            if (replies != null)
            {
                result.Replies = new ReplyListType();
                foreach (var reply in replies)
                {
                    result.Replies.Add(MapToDataObject(reply.Item1, reply.Item2));
                }
            }
            return result;
        }
    }
}
